// pages/find/card/card-list.js
var app = getApp()

const keepList = "member-visiting-card/keep-list"
const likeList = "member-visiting-card/like-list"
Page({
    /**
     * 页面的初始数据
     */
    data: {
        page: 1,
        total: 2,
        list: [],
        flag: 2,
        loading: false,
        reqNum: 0
    },

    /**
     * 生命周期函数--监听页面加载
     * flag=1 点赞列表  flag=2名片列表
     * 
     */
    onLoad: function (options) {
        this.setData({
            flag: options.flag
        })

    },
    /**
     * 加载收藏列表
     */
    keepList: function () {
        if (this.data.page > this.data.total || this.data.loading) {
            return;
        }
        this.setData({ loading: true })
        app.http.get(keepList, {
            data: {
                page: this.data.page
            }
        }).then(res => {
            this.setData({
                page: this.data.page + 1,
                loading: false,
                list: this.data.list.concat(res.data.items),
                num: res.data.num,
                total: res.data.total,
                reqNum: res.data.audit_num
            });
        }).catch(err => {
            app.wxToast.error(err.message);
        })
    },
    /**
     * 加载点赞列表
     */
    likeList: function () {
        if (this.data.page > this.data.total) {
            return;
        }
        this.setData({ loading: true })
        app.http.get(likeList, {
            data: {
                page: this.data.page
            }
        }).then(res => {
            this.setData({
                page: this.data.page + 1,
                loading: false,
                list: this.data.list.concat(res.data.items),
                num: res.data.num,
                total: res.data.total,

            });
        }).catch(err => {
            app.wxToast.error(err.message);
        })
    },
    /**
     * 搜索输入框
     */
    searchInput: function (e) {
        this.setData({
            searchValue: e.detail.value,
            page: 1
        })
        if (e.detail.value.length <= 0) {
            if (this.data.flag == 1) {
                this.likeList();
            } else {
                this.keepList();
            }
        }
    },
    /**
     * 搜索
     */
    search: function () {
        if (this.data.page > this.data.total) {
            return;
        }
        if (this.data.page == 1) {
            this.setData({
                list: [],
                num: 0
            })
        }
        this.setData({ loading: true })
        var reqUrl = '';
        if (this.data.flag == 1) {
            reqUrl = likeList;
        } else {
            reqUrl = keepList;
        }
        app.http.get(reqUrl, {
            data: {
                data: this.data.searchValue,
                page: this.data.page
            }
        }).then(res => {

            //搜不到total会边成0
            if (res.data.total == 0) {
                this.setData({ total: 2 })
            } else {
                this.setData({ total: res.data.total })
            }

            this.setData({
                page: this.data.page + 1,
                loading: false,
                list: this.data.list.concat(res.data.items),
                total: this.data.total,
                num: res.data.num
            });
        }).catch(err => {
            app.wxToast.error(err.message);
        })
    },


    /**
     * 拨打电话
     */
    callPhone: function (event) {
        wx.makePhoneCall({
            phoneNumber: event.currentTarget.dataset.phone //仅为示例，并非真实的电话号码
        })
    },
    /**
     * 查看别人名片
     */
    detail: function (event) {
        wx.navigateTo({
            url: 'other-card?id=' + event.currentTarget.dataset.id
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (this.data.searchValue != '' && this.data.searchValue != undefined) {//搜索
            this.search();
        } else {
            if (this.data.flag == 1) {
                this.likeList();
            } else {
                this.keepList();
            }
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        if (this.data.flag == 1) {
            this.likeList();
        } else {
            this.keepList();
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.searchValue != '' && this.data.searchValue != undefined) {//搜索
            this.search();
        } else {
            if (this.data.flag == 1) {
                this.likeList();
            } else {
                this.keepList();
            }
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    }
})